﻿using System;
using System.Collections.Generic;
using System.Configuration;

namespace CML.Intercamber.Service
{

    /// <summary>
    /// Cette classe permet de récupérer les données du fichier de conf, correspondant à toutes les classes ci dessous
    /// </summary>
    public class IntercambersTasksConfig
    {
        public static Dictionary<string, IntercambersTasksConfigElement> Configs;
        public static void LoadIntercambersTasksConfig()
        {
            Configs = new Dictionary<string, IntercambersTasksConfigElement>();
            IntercambersTasksConfigSection sec = (IntercambersTasksConfigSection)ConfigurationManager.GetSection("intercambersTasks");
            foreach (IntercambersTasksConfigElement i in sec.Instances)
            {
                Configs.Add(i.Name, i);
            }
        }
        public static IntercambersTasksConfigElement GetConfig(string instanceName)
        {
            return Configs[instanceName];
        }
    }


    public class IntercambersTasksConfigSection : ConfigurationSection
    {
        [ConfigurationProperty("", IsRequired = true, IsDefaultCollection = true)]
        public IntercambersTasksConfigCollection Instances
        {
            get { return (IntercambersTasksConfigCollection)this[""]; }
            set { this[""] = value; }
        }
    }

    public class IntercambersTasksConfigCollection : ConfigurationElementCollection
    {
        protected override ConfigurationElement CreateNewElement()
        {
            return new IntercambersTasksConfigElement();
        }
        protected override object GetElementKey(ConfigurationElement element)
        {
            return ((IntercambersTasksConfigElement)element).Name;
        }
    }

    public class IntercambersTasksConfigElement : ConfigurationElement
    {
        [ConfigurationProperty("name", IsKey = true, IsRequired = true)]
        public string Name
        {
            get { return (string)base["name"]; }
        }

        [ConfigurationProperty("active", IsRequired = true)]
        public int Active
        {
            get { return Convert.ToInt32(base["active"]); }
        }

        [ConfigurationProperty("cronSchedule", IsRequired = true)]
        public string CronSchedule
        {
            get { return (string)base["cronSchedule"]; }
        }

        [ConfigurationProperty("threadNumbers", IsRequired = true)]
        public int ThreadNumbers
        {
            get { return Convert.ToInt32(base["threadNumbers"]); }
        }

        [ConfigurationProperty("itemsPerThread", IsRequired = true)]
        public int ItemsPerThread
        {
            get { return Convert.ToInt32(base["itemsPerThread"]); }
        }

        [ConfigurationProperty("label", IsRequired = true)]
        public string Label
        {
            get { return (string)base["label"]; }
        }

    }
}
